//
//  PreAuthorizePopViewController.m
//  PPDL
//
//  Created by 王洋 on 2019/8/26.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "PreAuthModel.h"
#import "PreAuthItemCell.h"
#import "PreAuthorizePopViewController.h"

#define PreAuth_VCWidth           TTScreenWidth - 100

#define PreAuth_HeaderHeight         100
#define PreAuth_FooterHeight         50
#define PreAuth_CellItemHeight       60


@interface PreAuthorizePopViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray <PreAuthModel *> *preAuthItemListArray;

@property (nonatomic ,strong) UIViewController *vc;

@property (nonatomic, assign) ETTAuthorizationType defaultAuthType;
@property (nonatomic, assign) ETTAuthorizationType selectAuthType;

@property (nonatomic, assign) CGFloat   defaultHeight;

@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIView *footerView;

@end

@implementation PreAuthorizePopViewController

- (void)dealloc {
    
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self initPop];
    [self setUpUI];
    [self prepareData];
}

- (void)prepareData {
    
    self.preAuthItemListArray = @[].mutableCopy;
    
    for (int i = 0; i < 2; i++) {
        
        PreAuthModel *model = [[PreAuthModel alloc] init];
        model.itemTitle = i%2==0?@"支付宝预授权":@"余额押金";
        model.preAuthStyle = i%2==0?(ETTAuthorization_AlipayAuth):(ETTAuthorization_DepositAuth);
        
        if (self.defaultAuthType == i) {
            model.isSelect = YES;
        } else {
            model.isSelect = NO;
        }
        
        [self.preAuthItemListArray addObject:model];
    }
    [self.tableView reloadData];

}

- (void)initPop {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.defaultHeight = self.preAuthItemListArray.count * PreAuth_CellItemHeight + PreAuth_HeaderHeight + PreAuth_FooterHeight;
    
    if (self.defaultHeight > TTScreenHeight / 2) {
        self.defaultHeight = TTScreenHeight / 2;
    }
    
    self.contentSizeInPopup = CGSizeMake(PreAuth_VCWidth, self.defaultHeight);
    self.popupController.navigationBarHidden = YES;
    [self.popupController.backgroundView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTap)]];
}

- (void)setUpUI {
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.headerView];
    [self.view addSubview:self.footerView];
    
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(PreAuth_HeaderHeight);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(PreAuth_HeaderHeight);
        make.bottom.mas_equalTo(- PreAuth_FooterHeight);
    }];
    
    [self.footerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(PreAuth_FooterHeight);
    }];
    
}

-(void)closeBlockView {
    [self backgroundTap];
}

- (void)backgroundTap  {
    [self.popupController dismiss];
}

- (void)btnOnClicked:(UIButton *)btn {
    
    if (btn.tag == 1000) {
        
        if (self.selectType) {
            self.selectType(ETTAuthorization_None);
        }
    } else {
        if (self.selectType) {
            self.selectType(self.selectAuthType);
        }
    }
    
    [self closeBlockView];
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.scrollEnabled = NO;
        //银行卡cell
        [_tableView registerNib:[UINib nibWithNibName:@"PreAuthItemCell" bundle:nil] forCellReuseIdentifier:@"PreAuthItemCellID"];
    }
    return _tableView;
}

- (UIView *)headerView {
    if (!_headerView) {
        
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, TTScreenWidth, PreAuth_HeaderHeight)];
        _headerView.backgroundColor = [UIColor whiteColor];;
        
        UIView *lineView = [UIView new];
        lineView.backgroundColor = [UIColor lightGrayColor];
        [_headerView addSubview:lineView];
        
        UILabel *title = [[UILabel alloc] init];
        title.text = @"提示";
        title.textAlignment = NSTextAlignmentCenter;
        [_headerView addSubview:title];
        
        UILabel *memo = [UILabel new];
        memo.text = @"您尚未充值保证金，无法参拍，请充值保证金(300元)或者开通支付宝预授权。";
        memo.font = [UIFont systemFontOfSize:15];
        memo.numberOfLines = 2;
        memo.textAlignment = NSTextAlignmentLeft;
        [_headerView addSubview:memo];

        [title mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(15);
        }];
        
        [memo mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.top.mas_equalTo(20);
            make.bottom.mas_equalTo(10);
        }];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(0);
            make.height.mas_equalTo(0.5);
        }];        
        
    }
    return _headerView;
}

- (UIView *)footerView {
    
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, TTScreenWidth, 50)];
        _footerView.backgroundColor = [UIColor whiteColor];;
        
        UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancleBtn setTitle:@"朕知道了" forState:UIControlStateNormal];
        [cancleBtn setBackgroundColor:[UIColor redColor]];
        cancleBtn.tag = 1000;
        [cancleBtn addTarget:self action:@selector(btnOnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cancleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_footerView addSubview:cancleBtn];
        
        UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [sureBtn setBackgroundColor:[UIColor redColor]];
        sureBtn.tag = 2000;
        [sureBtn addTarget:self action:@selector(btnOnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_footerView addSubview:sureBtn];
        
        [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.mas_equalTo(0);
            make.width.mas_equalTo((PreAuth_VCWidth-1)/2);
            make.height.mas_equalTo(PreAuth_FooterHeight);
        }];
        
        [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.bottom.mas_equalTo(0);
            make.width.mas_equalTo((PreAuth_VCWidth-1)/2);
            make.height.mas_equalTo(PreAuth_FooterHeight);
        }];

    }
    return _footerView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.preAuthItemListArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
        PreAuthItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PreAuthItemCellID" forIndexPath:indexPath];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    
        [cell fillCellWithModel:self.preAuthItemListArray[indexPath.row]];
//
//        cell.refreshItemModel = ^(PreAuthModel * _Nonnull model) {
//            
//        };
        
        return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return PreAuth_CellItemHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    

    for (int i = 0; i<self.preAuthItemListArray.count; i++) {
        self.preAuthItemListArray[i].isSelect = NO;
        if (i == indexPath.row) {
            //用于展示
            self.preAuthItemListArray[indexPath.row].isSelect = !self.preAuthItemListArray[indexPath.row].isSelect;
            //用于传出
            self.selectAuthType = (ETTAuthorizationType)indexPath.row;
        }
    }
    
    [self.tableView reloadData];


}

- (instancetype)initDefaultIndex:(ETTAuthorizationType)type defaultHeight:(CGFloat)height vc:(UIViewController *)vc dataSource:(NSMutableArray <PreAuthModel *>*)dataSource {

    self.preAuthItemListArray = @[].mutableCopy;
    if (self = [super init]) {
        self.vc = vc;
        self.preAuthItemListArray = dataSource.mutableCopy;
        self.defaultHeight = height;
    }
    return self;
}


@end
